package com.qfedu.vhr.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.vhr.common.RespBean;
import com.qfedu.vhr.system.entity.Position;
import com.qfedu.vhr.system.mapper.PositionMapper;
import com.qfedu.vhr.system.service.IPositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-01-10
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {

    @Override
    public RespBean addPosition(Position position) {
        QueryWrapper<Position> qw = new QueryWrapper<>();
        qw.lambda().eq(Position::getName, position.getName());
        Position one = getOne(qw);
        if (one != null) {
            return RespBean.error("职位名称重复，添加失败");
        }
        position.setEnabled(true);
        position.setCreateDate(LocalDateTime.now());
        return save(position) ? RespBean.ok("添加成功") : RespBean.error("添加失败");
    }

    private boolean checkPositionNameRepeat(Position position) {
        QueryWrapper<Position> qw = new QueryWrapper<>();
        qw.lambda().eq(Position::getName, position.getName());
        return getOne(qw) != null;
    }

    @Override
    public RespBean updatePosition(Position position) {
        QueryWrapper<Position> qw = new QueryWrapper<>();
        qw.lambda().eq(Position::getName, position.getName()).ne(Position::getId, position.getId());
        Position one = getOne(qw);
        if (one != null) {
            return RespBean.error("职位名称重复，更新失败");
        }
        return updateById(position) ? RespBean.ok("更新成功") : RespBean.error("更新失败");
    }

}
